package com.apes.fn.rpt.capital;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.query.ApesDao;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author pengyb
 */
@Service("capitalReportService")
public class CapitalReportService extends ApesDao {
    @Autowired
    CapitalReportDao capitalReportDao;

    public JSONObject queryCapitalReport(SimpleRequest request) {

        JSONObject requestJO = request.getJO();
        requestJO.put("ROLE", request.getSession().getPostId());
        requestJO.put("DATE_START", DateUtil.format(requestJO.getDate("DATE_START"), DateUtil.SHOW_DATE_FORMAT));
        requestJO.put("DATE_END", DateUtil.format(requestJO.getDate("DATE_END"), DateUtil.SHOW_DATE_FORMAT));
        JSONObject capitalReport = new JSONObject();
        //收款方式汇总(货币资金+应收)
        JSONArray paymentMethod = capitalReportDao.queryPaymentMethod(requestJO);
        capitalReport.put("PaymentMethod", paymentMethod);
        capitalReport.put("PaymentMethodTotal", paymentMethod.stream().map(p -> ((JSONObject) p).getDoubleValue("AMOUNT")).reduce(Arith::add).orElse(0.0));
        //查询销售报表
        JSONObject salesReport = capitalReportDao.queryDailySalesReport(requestJO).getJSONObject(0);
        //营业收入汇总
        List<Map> businessIncome = Arrays.asList(MapUtil.mapper("TYPE", "服务业绩", "AMOUNT", salesReport.getDoubleValue("SERVICE_PERFORMANCE")),
//                MapUtil.mapper("TYPE", "定损直赔", "AMOUNT", salesReport.getDoubleValue("FIXED_LOSS_DIRECT_COMPENSATION")),
                MapUtil.mapper("TYPE", "接案介绍费", "AMOUNT", salesReport.getDoubleValue("FN_SETTLE_EXPENS")),
                MapUtil.mapper("TYPE", "年审", "AMOUNT", salesReport.getDoubleValue("ANNUAL_AUDIT")),
                MapUtil.mapper("TYPE", "车险佣金", "AMOUNT", salesReport.getDoubleValue("FN_COMMISSION")),
                MapUtil.mapper("TYPE", "二手车佣金", "AMOUNT", salesReport.getDoubleValue("USER_CAR_FEE")),
                MapUtil.mapper("TYPE", "资金收入单", "AMOUNT", salesReport.getDoubleValue("CAPITAL_INCOME"))
        );
        capitalReport.put("BusinessIncome", businessIncome);
        capitalReport.put("BusinessIncomeTotal", businessIncome.stream().map(m-> (double) m.get("AMOUNT")).reduce(Arith::add).orElse(0.0));

        //预收款汇总
        List<Map> advancesReceived = Arrays.asList(MapUtil.mapper("TYPE", "年卡销售", "AMOUNT", salesReport.getDoubleValue("BUY_YEARCARD")),
                MapUtil.mapper("TYPE", "年卡消费", "AMOUNT", salesReport.getDoubleValue("YEARCARD")),
                MapUtil.mapper("TYPE", "收储值", "AMOUNT", salesReport.getDoubleValue("STORED_VALUE_RECHARGE")),
                MapUtil.mapper("TYPE", "使用储值", "AMOUNT", salesReport.getDoubleValue("STORED_VALUE")),
                MapUtil.mapper("TYPE", "收订金", "AMOUNT", salesReport.getDoubleValue("ADVANCE_PAYMENT_RECHARGE")),
                MapUtil.mapper("TYPE", "用订金", "AMOUNT", salesReport.getDoubleValue("ADVANCE_PAYMENT"))
        );
        capitalReport.put("AdvancesReceived", advancesReceived);
        capitalReport.put("AdvancesReceivedTotal", advancesReceived.stream().map(m-> (double) m.get("AMOUNT")).reduce(Arith::add).orElse(0.0));

        //会员联盟汇总
        List<Map> crossCompany = Arrays.asList(MapUtil.mapper("TYPE", "其它门店会员来我店消费", "AMOUNT", salesReport.getDoubleValue("CROSS_COMPANY_INCOME")),
                MapUtil.mapper("TYPE", "我店会员去其它门店消费", "AMOUNT", salesReport.getDoubleValue("CROSS_COMPANY_PAY")));
        capitalReport.put("CrossCompany", crossCompany);
        capitalReport.put("CrossCompanyTotal", crossCompany.stream().map(m-> (double) m.get("AMOUNT")).reduce(Arith::add).orElse(0.0));
        //代收款汇总
        capitalReport.put("LossCollection", Arrays.asList(MapUtil.mapper("TYPE", "定损代收", "AMOUNT", salesReport.getDoubleValue("FIXED_LOSS_COLLECTION"))));
        capitalReport.put("LossCollectionTotal", salesReport.getDoubleValue("FIXED_LOSS_COLLECTION"));
        capitalReport.put("IncomeTotal", Arith.add(Arith.add(capitalReport.getDoubleValue("BusinessIncomeTotal"), capitalReport.getDoubleValue("AdvancesReceivedTotal")), capitalReport.getDoubleValue("CrossCompanyTotal")));

        return capitalReport;
    }

    /***
     * 处理前端时间
     */

    private Date parseDateString(String str) {
        str = str.replace("Z", " UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(str);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
